package net.huashitong.ssydt.provider.pk.service;


import net.huashitong.ssydt.base.dao.LongMapper;
import net.huashitong.ssydt.base.service.AbstractLongService;
import net.huashitong.ssydt.exam.entity.SsExamQuestion;
import net.huashitong.ssydt.pk.entity.SsPkYmdxQuestionRelation;
import net.huashitong.ssydt.provider.exam.service.SsExamQuestionService;
import net.huashitong.ssydt.provider.pk.mapper.SsPkYmdxQuestionRelationMapper;
import net.huashitong.ssydt.provider.utils.date.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.12.12
 */
@Service
@Transactional
public class SsPkYmdxQuestionRelationServiceImpl extends AbstractLongService<SsPkYmdxQuestionRelation>
        implements SsPkYmdxQuestionRelationService {

    private Logger logger = LoggerFactory.getLogger(SsPkYmdxQuestionRelationService.class);

    private SsPkYmdxQuestionRelationMapper mapper;

    private SsExamQuestionService ssExamQuestionService;

    @Autowired
    public SsPkYmdxQuestionRelationServiceImpl(SsPkYmdxQuestionRelationMapper mapper,
                                               SsExamQuestionService ssExamQuestionService) {
        this.mapper = mapper;
        this.ssExamQuestionService = ssExamQuestionService;
    }

    @Override
    protected LongMapper<SsPkYmdxQuestionRelation> getLongMapper() {
        return this.mapper;
    }

    @Override
    public List<SsPkYmdxQuestionRelation> findNewest() {
        List<SsPkYmdxQuestionRelation> list = null;
        if ("12:00:00".compareTo(DateUtils.getTime()) >= 0) {
            String date = DateUtils.getPastDayStr(DateUtils.getDate(), -1);
            list = this.mapper.queryFiveQuestionByBelongDate(date);
        }
        if (list == null || list.isEmpty()) {
            list = this.mapper.queryFiveQuestionByNewest();
        }
        return list;
    }

    @Override
    @Scheduled(cron = "0 50 11 * * ?")
    public void autoUpdateLastNewQuestion() {
        List<SsPkYmdxQuestionRelation> ssPkYmdxQuestionRelations = new ArrayList<>();

        List<SsExamQuestion> questions = ssExamQuestionService.findFiveQuestionByNowDate(DateUtils.getDate());
        for (SsExamQuestion question : questions) {
            SsPkYmdxQuestionRelation relation = new SsPkYmdxQuestionRelation();
            relation.setBelongDate(question.getBelongDate());
            relation.setQuestionId(question.getId());
            ssPkYmdxQuestionRelations.add(relation);
        }
        int number = 0;
        if (!ssPkYmdxQuestionRelations.isEmpty()) {
            number = this.batchInsert(ssPkYmdxQuestionRelations);
        }
        logger.info("一马当先，定时任务。执行题库更新，新增{}条记录", number);
    }

    @Override
    public int batchInsert(List<SsPkYmdxQuestionRelation> ssPkYmdxQuestionRelations) {
        return this.mapper.batchInsert(ssPkYmdxQuestionRelations);
    }


}
